package com.zkny.zknyerp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zkny.zknyerp.mapper.DeviceMapper;
import com.zkny.zknyerp.mapper.ModelMapper;
import com.zkny.zknyerp.mapper.ScenarioMapper;
import com.zkny.zknyerp.mapper.TypeMapper;
import com.zkny.zknyerp.model.Device;
import com.zkny.zknyerp.model.Model;
import com.zkny.zknyerp.model.Scenario;
import com.zkny.zknyerp.model.Type;
import com.zkny.zknyerp.service.DeviceService;
import com.zkny.zknyerp.utils.Constant;
import com.zkny.zknyerp.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.UUID;

/**
 * 用户服务接口实现类
 */
@Service
@Transactional
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private ModelMapper modelMapper;
    @Autowired
    private ScenarioMapper scenarioMapper;
    @Autowired
    private TypeMapper typeMapper;


    /**
     * 增添设备接口
     * @param device
     * @return
     */
    public Result addDevice(Device device){
        Example example = new Example(Device.class);
        example.createCriteria().andEqualTo("imei",device.getImei());
        Device device1 = deviceMapper.selectOneByExample(example);
        if(!ObjectUtils.isEmpty(device1)){
            return new Result(Constant.code_1016,Constant.msg_1016);
        }
        UUID uuid = UUID.randomUUID();
        String[] keys = uuid.toString().split("-");
        device.setDevkey(keys[keys.length-1]);
        device.setStatus("1");
        int i = deviceMapper.insert(device);
        if(0 == i){
            return new Result(Constant.code_9999,Constant.msg_9999);
        }
        return new Result(Constant.code_0000,Constant.msg_0000);
    }

    /**
     * 查询设备接口
     * @param page
     * @param limit
     * @return
     */
    public Result getDevice(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Device> list = deviceMapper.selectAll();
        if (ObjectUtils.isEmpty(list)){
            return new Result(Constant.code_1002,Constant.msg_1002);
        }

        PageInfo<Device> pageInfo = new PageInfo<Device>(list);
        return new Result(Constant.code_0000,Constant.msg_0000,pageInfo);
    }

    /**
     * 根据IMEI详细查询
     */
    public Result getImei(String imei,Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        Example example = new Example(Device.class);
        example.createCriteria().andEqualTo("imei",imei);
        List<Device> list= deviceMapper.selectByExample(example);
        if(ObjectUtils.isEmpty(list)){
            return new Result(Constant.code_1002,Constant.msg_1002);
        }
        PageInfo<Device> pageInfo = new PageInfo<Device>(list);
        return new Result(Constant.code_0000,Constant.msg_0000,pageInfo);
    }

    /**
     * 设备删除接口
     */
    public Result deleteDevice(Device device){
        int i = deviceMapper.deleteByPrimaryKey(device);
        if(0 == i){
            return new Result(Constant.code_9999,Constant.msg_9999);
        }
        return new Result(Constant.code_0000,Constant.msg_0000);
    }

    /**
     * 修改设备信息
     * @param device
     * @return
     */
    public Result updateDevice(Device device){
        int i = deviceMapper.updateByPrimaryKeySelective(device);
        if(0 == i ){
            return new Result(Constant.code_9999,Constant.msg_9999);
        }
        return new Result(Constant.code_0000,Constant.msg_0000);
    }

    /**
     * 根据id查询设备信息
     */
    public Result getDev(Integer id){
        Example example = new Example(Device.class);
        example.createCriteria().andEqualTo("id",id);
        Device device = deviceMapper.selectOneByExample(example);
        if(ObjectUtils.isEmpty(device)){
            return new Result(Constant.code_9999,Constant.msg_9999);
        }
        return new Result(Constant.code_0000,Constant.msg_0000,device);
    }

    /**
     * 根据imei查询设备信息
     */
    public Result getAppDevice(String imei){
        Example example = new Example(Device.class);
        example.createCriteria().andEqualTo("imei",imei);
        Device device = deviceMapper.selectOneByExample(example);
        if (ObjectUtils.isEmpty(device)){
            return new Result(Constant.code_1002,Constant.msg_1002);
        }

        Example example1 = new Example(Model.class);
        example1.createCriteria().andEqualTo("id",device.getModelid());
        Model model = modelMapper.selectOneByExample(example1);
        if(ObjectUtils.isEmpty(model)){
            return new Result(Constant.code_1002,Constant.msg_1002);
        }
        device.setModel(model.getModel());

        Example example2 = new Example(Scenario.class);
        example2.createCriteria().andEqualTo("id",device.getScenarioid());
        Scenario scenario = scenarioMapper.selectOneByExample(example2);
        if(ObjectUtils.isEmpty(scenario)){
            return new Result(Constant.code_1002,Constant.msg_1002);
        }
        device.setScenario(scenario.getScenario());

        Example example3 = new Example(Type.class);
        example3.createCriteria().andEqualTo("id",device.getTypeid());
        Type type = typeMapper.selectOneByExample(example3);
        if(ObjectUtils.isEmpty(type)){
            return new Result(Constant.code_1002,Constant.msg_1002);
        }
        device.setType(type.getTname());

        return new Result(Constant.code_0000,Constant.msg_0000,device);
    }

    /**
     * 验证码校验
     */
    public Result getDevkey(String imei,String devkey){
        Example example = new Example(Device.class);
        example.createCriteria().andEqualTo("imei",imei);
        Device device = deviceMapper.selectOneByExample(example);
        if (ObjectUtils.isEmpty(device)){
            return new Result(Constant.code_1002,Constant.msg_1002);
        }
        if(!devkey.equals(device.getDevkey())){
            return new Result(Constant.code_1020,Constant.msg_1020);
        }
        return new Result(Constant.code_0000,Constant.msg_0000);
    }

}
